package spzx.task;

import org.joda.time.DateTime;
import com.atguigu.spzx.model.entity.order.OrderStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import spzx.mapper.OrderInfoMapper;


@Component
public class OrderStaTask {

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    //“2 * * * * ?” --> 每分钟的第二秒执行一次
    @Scheduled(cron = "2 * * * * ?")
    public void task(){
        //计算前一天的日期
        String preDate = new DateTime().plusDays(-1).toString("yyyy-MM-dd");

        //在订单统计表中判断前一天的统计数据是否存在
        Integer count = orderInfoMapper.checkPreDate(preDate);
        if(count > 0){
            System.out.println("前一天的订单统计数据已存在");
            return;
        }

        //从订单表order_info中统计前一天的数据，添加到order_statistics表中
        OrderStatistics orderStatistics = orderInfoMapper.getOrderStatisticsByPreDate(preDate);
        //订单统计数据不为空并且该数据在订单统计表中不存在时，添加到订单统计表中
        if(orderStatistics!= null){
            orderInfoMapper.addOrderStatistics(orderStatistics);
        }

    }
}
